const toggle = this.pageObj.getApiWidgetByAliasName('toggle');
const process = this.pageObj.getApiWidgetByAliasName('modal-process');
const prefix = this.pageObj.getVariables().find(v => v.name === 'environmentType')?.value || '';
const $form = this.getApiPage().getApiWidgetByAliasName('judge-content').widgetObj;
const three = this.pageObj.getApiWidgetByAliasName('三维组件')?.widgetObj
const { dataSource } = $form._option;
const url = new URLSearchParams(window.location.href);
let personDetail = JSON.parse(url && url.get('personDetail') || '{}');
const Modal = process.widgetObj.getModal();
const configs = process?.widgetObj.configs()
const taskTypeEnum = { 信息发布: '1', 信息上报: '2', 下发任务: '10' };
console.log($event, configs)
function baseGet(url) {
    const headers = { "Content-type": "application/json" };
    return fetch(url, { headers }).then(res => res.json())
}
function basePost(url, eventId) {
    const option = {
        method: 'POST',
        headers: {
            "Content-type": "application/x-www-form-urlencoded",
        },
        body: 'eventId=' + eventId
    }
    return fetch(url, option).then(res => res.json())
}
const defaultData = {
    eventId: dataSource.id,
    eventTitle: dataSource.eventTitle,
    projectCode: dataSource.projectCode,

    taskTypeId: taskTypeEnum[$event],
    taskTypeName: $event,
    // 下发部门
    taskIssuDepart: personDetail.deptCode,
    taskIssuDepartName: personDetail.deptName,

    // 下发人
    taskIssuer: personDetail.id,
    taskIssuerName: personDetail.personName,

    // 记录人
    takenotesPersonName: personDetail.personName,
    takenotesPersonId: personDetail.id,

    progress: dataSource.whetherProcess,
}

let config;
let style = { width: 600, height: 400 };
switch ($event) {
    case '记录动态':
        config = configs['记录动态'](1);
        break;
    case '误警事件':
        config = configs['自定义内容']('提示：是否确定为误警事件');
        break;
    case '解除事件':
        config = configs['自定义内容']('提示：是否确定解除事件');
        break;
    case '关闭事件':
        config = configs['自定义内容']('提示：是否确定关闭事件');
        break;
    case '一键会议':
        const artemis = three?.artemis;
        const data = this.pageObj.getVariables().find(v => v.name === "screen_params")?.value
        const eventParams = JSON.parse(data)

        if (artemis && eventParams.id) {
            basePost(`/${prefix}SummaryAssessmentSV/emergencyHandle/convergedCommunication`, eventParams.id).then(res => {
                if (res.success) {
                    if (res.result.length) {
                        const onlineMembers = artemis.getOnlineMembers()
                        let __online = 0
                        res.result.forEach(item => {
                            item.userId = item.communicationAccount;
                            item.username = item.communicationName;
                            if (onlineMembers.has(item.userId)) {
                                artemis.addMember(item);
                                __online ++;
                            } else {
                                layer.msg(`成员${item.username}不在线！`)
                            }
                        });

                        if (__online > 0) {
                            artemis.createVideoMeeting();
                        } else {
                            layer?.msg('没有成员在线！');
                        }
                    } else {
                        layer?.msg('没有找到人员！')
                    }
                }
            })
        }

        break;
    case '录像回放':
        // 回放
        const $playback = this.pageObj.getApiWidgetByAliasName('playback');
        let info = {}
        if(dataSource.eventDetails) {
            info = JSON.parse(dataSource.eventDetails);
        }

        console.log('info', info);
        if (info?.jpbPath) {
            $playback.widgetObj.setData([{ data: info?.jpbPath }]);
            $playback.showWidget();
            $playback.widgetObj.render();
        } else {
            $playback.hideWidget();
            window.layer.msg('暂无录像回放')
        }
        break;
    case '实时画面':
        if (dataSource.sourceDeviceId) {
            // 打开实时视频
            baseGet(`/${prefix}Basics/basics-main/system/equipmentObject/listDetailByEquipmentCode?equipmentCode=${dataSource.sourceDeviceId}`)
                .then(({ success, result } = {}) => {
                    if (success && result) {
                        const { rows } = result;
                        const data = rows[0]?.equipmentConfig?.gbVideoConfig;
                        var host = this.pageObj.getVariables().find(x => x.name == "stream_url").value;
                        three.sullivan.close();
                        if (data && data.sipUser && data.videoChannelId) {
                            three.sullivan.addPlayer(`${host}/api/v1/stream/rtc/play`, `${data.sipUser}/${data.videoChannelId}`)
                            three.sullivan.initial()
                        } else {
                            window.layer.msg('暂无设备号/通道号')
                        }
                    }
                })
        } else {
            three.sullivan.close();
            window.layer.msg('未关联摄像头')
        }
        break;
    default:
        config = configs[$event](1);
        if ($event === '协同会商') {
            style.width = 650;
            style.height = 450;
        } else {
            style.height = 500;
        }
}

if (config) {
    Modal.type = $event;
    process?.widgetObj.renderModal(config, defaultData);
    process?.widgetObj.show(style);
}

toggle?.widgetObj.toggle()